package com.house.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.house.biz.DistrictBiz;
import com.house.biz.HouseBiz;
import com.house.biz.MessageBiz;
import com.house.biz.PhotoBiz;
import com.house.biz.StreetBiz;
import com.house.biz.TypeBiz;
import com.house.biz.impl.DistrictBizImpl;
import com.house.entity.District;
import com.house.entity.House;
import com.house.entity.Photo;
import com.house.entity.Street;
import com.house.entity.Type;

@Controller
public class IndexController {

	@Autowired
	private DistrictBiz districtBiz;

	@Autowired
	private HouseBiz houseBiz;
	
	@Autowired
	private StreetBiz streetBiz;
	
	@Autowired
	private MessageBiz messageBiz;
	
	@Autowired
	private TypeBiz typeBiz;
	
	@Autowired
	private PhotoBiz photoBiz;

	@RequestMapping({"/index","/"})
	public String index(Model model, HttpServletRequest request) {
		List<District> districts = districtBiz.getAllDistricts();
		request.getSession().setAttribute("districts", districts);

		
		
		List<House> houses = houseBiz.getAllHouses();
		model.addAttribute("houses", houses);
		
		List<Street> streets=streetBiz.getAllStreets();
		request.getSession().setAttribute("streets",streets);
		
		List<Type> types=typeBiz.getAllType();
		request.getSession().setAttribute("types",types);

		return "index";
	}

	@RequestMapping("/map")
	public String map() {
		return "map";
	}

	@RequestMapping("/getByDistrict")
	public String getByDistrict(Model model, int dId) {
		List<House> houses = houseBiz.getHousesByDistrict(dId);
		model.addAttribute("houses", houses);
		return "index";
	}
	
	@RequestMapping(value="/gethousesByConditions",method = RequestMethod.GET)
	public String gethousesByConditions(Model model,
			@RequestParam(value="districtId",required=false)int districtId,
			@RequestParam(value="streetId",required=false)int streetId,
			@RequestParam(value="typeId",required=false)int typeId,
			@RequestParam(value="topic",required=false)String topic){
		
		List<House> houses=houseBiz.gethousesByConditions(districtId, streetId, typeId,topic);
		model.addAttribute("houses", houses);
		return "index";
		
	}

}
